﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TruckScale.Helpers
{
  public class ControlDoubleClickHelper : DependencyObject
  {
    public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ControlDoubleClickHelper), new PropertyMetadata(OnCommandChanged));

    public static ICommand GetCommand(Control target)
    {
      return (ICommand)target.GetValue(CommandProperty);
    }

    public static void SetCommand(Control target, ICommand value)
    {
      target.SetValue(CommandProperty, value);
    }

    private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
      Control control = sender as Control;
      ICommand command = GetCommand(control);

      if (command.CanExecute(null))
      {
        command.Execute(null);
        e.Handled = true;
      }
    }

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      Control control = d as Control;
      control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
    }
  }
}
